package com.dsg.controller.apply;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsg.common.constants.MessageConstants;
import com.dsg.common.dto.AjaxResult;
import com.dsg.common.util.SingletonLoginUtils;
import com.dsg.common.util.toolbox.StringUtil;
import com.dsg.controller.base.BaseController;
import com.dsg.entity.apply.DsgApplyPay;
import com.dsg.entity.model.bank.DsgBankDetailModel;
import com.dsg.service.account.IDsgAccountSettingService;
import com.dsg.service.apply.IDeductOneYuanService;
import com.dsg.service.apply.IDsgApplyStatusService;
import com.dsg.service.approve.IDsgApproveCaseService;
import com.dsg.support.LeveldbAccess;
import com.dsg.util.RRException;
import com.dsg.workflow.WorkflowEngine;
import com.dsg.workflow.WorkflowService3;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: erp
 * @description: 扣1元
 * @author: 賴利榮
 * @create: 2020-10-08 15:09
 */
@RestController
@RequestMapping("/apply/deductOneYuan")
public class DeductOneYuanController extends BaseController {

    @Autowired
    private IDeductOneYuanService deductOneYuanService;
    @Autowired
    private IDsgApplyStatusService dsgApplyStatusService;
    @Autowired
    private IDsgAccountSettingService dsgAccountSettingService;
    @Autowired
    private LeveldbAccess leveldbAccess;
    @Autowired
    private IDsgApproveCaseService caseService;
    @Autowired
    private WorkflowEngine engine;

    /**
     * 查询列表（分页）
     *
     * @param dsgApplyPay
     * @return
     */
    @GetMapping(value = "/list")
    @ApiOperation("查询列表（分页）")
    public AjaxResult list(DsgApplyPay dsgApplyPay) {
        // 分页
        Page page = getPage();
        String sort = getParameter("sort");
        List<OrderItem> orderItemList = new ArrayList<>();
        if (StringUtil.isEmpty(sort)) {
            orderItemList.add(new OrderItem().setColumn("b.CREATE_TIME").setAsc(false));
            page.setOrders(orderItemList);
        }
        dsgApplyPay.setEntCode(SingletonLoginUtils.getSystemEntCode());
        dsgApplyPay.setNodeName(WorkflowService3.NODE_DEDUCT_ONE);
        page = deductOneYuanService.selectDeductOneYuanList(page, dsgApplyPay);
        // 格式要返回的数据
        AjaxResult ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, page.getRecords(), (int) page.getTotal());
        return ajaxResult;
    }

    /**
     * 批量修改是否扣$1元状态
     *
     * @return
     */
    @PutMapping(value = "/updateDEDUStatus")
    @ApiOperation("批量修改是否扣$1元状态")
    public AjaxResult updateDEDUStatus(@ApiParam(value = "id以，号分隔", name = "idList", required = true) @RequestParam String idList,
                                       @ApiParam(value = "isDedu", name = "isDedu", required = true) @RequestParam Integer isDedu) {
        deductOneYuanService.updateDEDUStatus(idList, isDedu);
        return result(MessageConstants.SSO_STATUS_SUCCESS);
    }

    @GetMapping(value = "/downloadFile")
    @ApiOperation("導出扣一元")
    public void downloadFile(HttpServletResponse response) throws Exception {
        deductOneYuanService.download(response);
    }

    /**
     * 上載
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/importExcel")
    @ApiOperation("上載")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult importExcel(@ApiParam(value = "file", name = "file", required = true) @RequestParam("file") MultipartFile file) {
        //保存文件
        try {
            EasyExcel.read(file.getInputStream(), DsgBankDetailModel.class, new UploadDeductOneYuan(deductOneYuanService,
                    dsgApplyStatusService, caseService, dsgAccountSettingService, engine, leveldbAccess)).sheet(0).headRowNumber(3).doRead();
        } catch (RRException re) {
            throw new RRException(re.getMessage());
        } catch (Exception e){
            throw new RRException("導入扣一元異常!");
        }
        return result(MessageConstants.SSO_STATUS_SUCCESS);
    }
}
